<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 17:27
 */
namespace app\admin\controller;
use app\models\Models;

class Activity extends Check{
    /**
     * 活动列表
     * @return \think\response\View
     */
    public function activityList(){
        $get = input('param.');
        $activity = new Models('activity');
        $type = new Models('activityType');
        // 条件查询
        $search = search($get,'a.name','a.begin_time','a.status');
        $where  = $search['condition'].' and is_delete = '.Models::DELETE_NOT;
        // 商家登录活动列表
        $group_arr = self::getGroup();
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $where .= ' and a.uid = '.$group_arr['id'];
            $flag = true;
        }
        if($get['type_id'] != ''){
            $where .= ' and a.type_id = '.$get['type_id'];
            $search['display']['type_id'] = $get['type_id'];
        }
        $join = [
            ['activity_type b','a.type_id = b.id','left'],
            ['business c','a.uid = c.id','left'],
            ['user d','a.uid = d.id','left'],
            ['business e','a.bid = e.id','left'],
        ];
        $field  = "a.*,b.name type_name,c.name business,c.background b_business,d.nickname,e.name location";
        $list = $activity->_page($where,$field,'a.id desc',$join,$get['page'],15,$get);
        $type = $type->_select();
        $config = self::getConfig();
        $pre = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'activity'. DS .'s';
        $pre1 = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'background'. DS .'s';
        return view('activityList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display'],'type'=>$type,'pre'=>$pre,'pre1'=>$pre1,'flag'=>$flag]);
    }

    /**
     * 添加活动
     * @return array|\think\response\View
     */
    public function activityAdd(){
        // 获取用户类型
        $group_arr = self::getGroup();
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = true;
        }
        if(request()->isAjax()){
            $post = input('param.');
            $activity = new Models('activity');
            try{
                if($group_arr['group_id'] != Models::GROUP_BUSINESS && empty($post['bid'])){
                    throw new \LogicException('活动地点不能为空',1010);
                }
                $add = [
                    'uid'         => $group_arr['group_id'] == Models::GROUP_BUSINESS ? $group_arr['id'] : 0,
                    'bid'         => $group_arr['group_id'] == Models::GROUP_BUSINESS ? $group_arr['id'] : $post['bid'],
                    'name'        => $post['name'],
                    'description' => $post['description'],
                    'join_max'    => $post['join_max'],
                    'type_id'     => $group_arr['group_id'] == Models::GROUP_BUSINESS ? Models::ACTIVITY_TYPE_BUSINESS : Models::ACTIVITY_TYPE_OFFICIAL,
                    'begin_time'  => strtotime($post['begin_time']),
                    'create_time' => time(),
                ];
                if($_FILES){
                    $add['image'] = json_encode(upload('activity'));
                }
                $id = $activity->_add($add);
                if(!$id){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $admin = new Models('admin');
        // 商家列表
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON,'b.id'=>['<>',Models::OFFICIAL]],'a.id,b.name');
        return view('activityAdd',['business'=>$business,'flag'=>$flag]);
    }

    /**
     * 修改活动
     * @return array|\think\response\View
     */
    public function activitySave(){
        $get = input('param.');
        $activity = new Models('activity');
        $info = $activity->_find($get['id']);
        if($info['type_id'] == Models::ACTIVITY_TYPE_BUSINESS){
            $flag = true;
        }
        if(request()->isAjax()){
            $post = input('param.');
            try{
                if($info['type_id'] != Models::ACTIVITY_TYPE_BUSINESS && empty($post['bid'])){
                    throw new \LogicException('活动地点不能为空',1010);
                }
                $data = [
                    'id'          => $post['id'],
                    'uid'         => $info['type_id'] != Models::ACTIVITY_TYPE_BUSINESS ? $post['bid'] : $info['uid'],
                    'name'        => $post['name'],
                    'description' => $post['description'],
                    'join_max'    => $post['join_max'],
                    'begin_time'  => strtotime($post['begin_time']),
                    //'end_time'    => strtotime($post['end_time']),
                ];
                if($_FILES){
                    $data['background'] = upload('activity');
                }
                $bool = $activity->_save($data);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $admin = new Models('admin');
        // 商家列表
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON,'b.id'=>['<>',Models::OFFICIAL]],'a.id,b.name');
        // 拼接图片
        $config = self::getConfig();
        $pre = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'activity'. DS .'s';
        return view('activitySave',['info'=>$info,'business'=>$business,'pre'=>$pre,'flag'=>$flag]);
    }

    /**
     * 删除
     * @return array
     */
    public function goodsDel(){
        if(request()->isAjax()){
            $post = input('param.');
            $activity = new Models('activity');
            try{
                $bool = $activity->setValue(['id'=>$post['id']],['is_delete'=>Models::DELETE_YES]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 启用
     * @return array
     */
    public function start(){
        if(request()->isAjax()){
            $post = input('param.');
            $activity = new Models('activity');
            try{
                $bool = $activity->setValue(['id'=>$post['id']],['status'=>Models::STATUS_ON]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function stop(){
        if(request()->isAjax()){
            $post = input('param.');
            $activity = new Models('activity');
            try{
                $bool = $activity->setValue(['id'=>$post['id']],['status'=>Models::STATUS_OFF]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}